class Main extends egret.DisplayObjectContainer {
    private hajime: Hajime;
    private DO: DeviceOrientation;

    private slide1: egret.Bitmap;
    private slide2: egret.Bitmap;
    private phone: egret.Bitmap;
    private drawhand: egret.Bitmap;
    private drawhint: egret.Bitmap;


    private BG: egret.Bitmap;
    private slideLR: egret.Bitmap;
    private door_left: egret.Bitmap;
    private door_right: egret.Bitmap;
    private lantern: Lantern;
    private cup: Cup;

    private word1;
    private word2;

    private endpart: EndPart;
    private musicControl: MusicControl;

    private slidePic: egret.Bitmap;


    private loadingUI: LoadingUI;

    private sound;
    public constructor() {
        super();
        MyData.initial();


        this.hajime = new Hajime();
        // this.hajime.addEventListener(LoadEvent.ResourceLoad, this.LoadComplete, this);

        this.hajime.addEventListener(LoadEvent.LoadingLoad, this.LoadingLoadComplete, this);
        this.hajime.addEventListener(LoadEvent.LoadingProgress, this.LoadingLoadProgress, this);

        StartMusic();
    }
    private LoadingLoadComplete(): void {
        this.loadingUI = new LoadingUI();
        this.addChild(this.loadingUI);
    }
    private LoadingLoadProgress(event: egret.Event): void {
        this.loadingUI.setProgress(event.data[0], event.data[1]);
    }

    private BG_part() {
        var bgpart = new egret.Bitmap(RES.getRes("cloundUp_png"));
        this.addChild(bgpart);
        egret.Tween.get(bgpart, { loop: true }).to({ x: -740 }, 10000)

        var bgpart2 = new egret.Bitmap(RES.getRes("cloundUp_png"));
        this.addChild(bgpart2);
        bgpart2.x = 740;
        egret.Tween.get(bgpart2, { loop: true }).to({ x: 0 }, 10000)

        var bgpart3 = new egret.Bitmap(RES.getRes("cloundDown_png"));
        this.addChild(bgpart3);
        egret.Tween.get(bgpart3, { loop: true }).to({ x: 740 }, 8000)

        var bgpart4 = new egret.Bitmap(RES.getRes("cloundDown_png"));
        this.addChild(bgpart4);
        bgpart4.x = -740;
        egret.Tween.get(bgpart4, { loop: true }).to({ x: 0 }, 8000)


        this.word1 = new egret.Bitmap(RES.getRes("word1_png"));
        this.word2 = new egret.Bitmap(RES.getRes("word2_png"));
        this.word1.alpha = 0; this.word2.alpha = 0;
        this.addChild(this.word1); this.addChild(this.word2);
        this.word1.x = 300; this.word1.y = 600;
        this.word2.x = 250; this.word2.y = 700;
    }
    private Hint() {
        this.slide1 = new egret.Bitmap(RES.getRes("slideLR_png"));
        this.addChild(this.slide1);
        this.slide1.x = 575;
        this.slide1.y = 500;
        egret.Tween.get(this.slide1, { loop: true }).to({ x: 555, alpha: 0.5 }, 400, egret.Ease.cubicInOut).to({ x: 575, alpha: 1 }, 500, egret.Ease.cubicInOut);

        this.slide2 = new egret.Bitmap(RES.getRes("slideLR_png"));
        this.addChild(this.slide2);
        this.slide2.x = 450;
        this.slide2.y = 770;
        this.slide2.rotation = 90;
        egret.Tween.get(this.slide2, { loop: true }).to({ y: 750, alpha: 0.5 }, 400, egret.Ease.cubicInOut).to({ y: 770, alpha: 1 }, 500, egret.Ease.cubicInOut);
        egret.Tween.pauseTweens(this.slide2);
        this.slide2.alpha = 0;

        this.slidePic = new egret.Bitmap(RES.getRes("slidePic_png"));
        this.addChild(this.slidePic);
        this.slidePic.x = 605; this.slidePic.y = 500;


        this.phone = new egret.Bitmap(RES.getRes("phone_png"));
        this.addChild(this.phone);
        this.phone.x = 300 + this.phone.width / 2;
        this.phone.y = 600 + this.phone.height;
        this.phone.anchorOffsetX = this.phone.width / 2; this.phone.anchorOffsetY = this.phone.height;
        this.phone.rotation = -10;
        egret.Tween.get(this.phone, { loop: true }).to({ rotation: 8 + Math.random() * 5 }, 200 + Math.random() * 300, egret.Ease.cubicInOut).to({ rotation: -10 }, 400 + 200 * Math.random(), egret.Ease.cubicInOut);
        this.phone.alpha = 0;

        this.drawhint = new egret.Bitmap(RES.getRes("drawhint_png"));
        this.addChild(this.drawhint);
        this.drawhint.x = 520 + this.drawhint.width / 2;
        this.drawhint.y = 300 + this.drawhint.height;
        this.drawhint.anchorOffsetX = this.drawhint.width / 2; this.drawhint.anchorOffsetY = this.drawhint.height / 2;
        this.drawhint.rotation = -5;
        this.drawhint.alpha = 0;

        this.drawhand = new egret.Bitmap(RES.getRes("drawhand_png"));
        this.addChild(this.drawhand);
        this.drawhand.x = 500 + this.drawhand.width / 2;
        this.drawhand.y = 280 + this.drawhand.height;
        this.drawhand.anchorOffsetX = this.drawhand.width / 2; this.drawhand.anchorOffsetY = this.drawhand.height;
        this.drawhand.rotation = 0;
        egret.Tween.get(this.drawhand, { loop: true }).to({ x: 500 + this.drawhand.width / 2 - 30 }, 200 + Math.random() * 300, egret.Ease.cubicInOut).to({ x: 500 + this.drawhand.width / 2 }, 400 + 200 * Math.random(), egret.Ease.cubicInOut);
        this.drawhand.alpha = 0;

    }


    private LoadComplete() {
        // this.sound=RES.getRes("shake_mp3");

        this.BG = new egret.Bitmap(RES.getRes("BG_png"));
        this.BG.width = 740; this.BG.height = 1136;
        this.addChild(this.BG);


        this.door_left = new egret.Bitmap(RES.getRes("door_left_png"));
        this.door_left.width = 746; this.door_left.height = 1136;
        this.addChild(this.door_left);

        this.door_right = new egret.Bitmap(RES.getRes("door_right_png"));
        this.door_right.width = 740; this.door_right.height = 1136;
        this.addChild(this.door_right);

        this.Hint()


        this.lantern = new Lantern();
        this.addChild(this.lantern);
        this.lantern.x = 740 / 2; this.lantern.y = 1136 / 2 - 300;
        this.lantern.addEventListener("OpenTheDoor", this.OpenTheDoor, this);
        this.lantern.addEventListener("SlideLR", this.SlideLR, this);

        this.BG_part();

        this.cup = new Cup();
        this.addChild(this.cup);
        this.cup.addEventListener("GoEnd", this.GoEnd, this);



        this.endpart = new EndPart();
        this.endpart.addEventListener("Again", this.Again, this);


        this.DO = new DeviceOrientation();
        this.DO.addEventListener("Begin_Draw", this.BeginDraw, this);
        this.addChild(this.DO);

        this.musicControl = new MusicControl(true);
        this.musicControl.x = 740 - 70; this.musicControl.y = 200;
        this.musicControl.scaleX = 0.3; this.musicControl.scaleY = 0.3;
        this.addChild(this.musicControl);
    }
    private SlideLR(e: LoadEvent) {
        if (e.data == -1) {
            egret.Tween.pauseTweens(this.slide1);
            this.slide1.alpha = 0;

            egret.Tween.resumeTweens(this.slide2);
            this.slide2.alpha = 1;

            this.slidePic.alpha = 0;
            egret.Tween.get(this.slidePic).to({ alpha: 1 }, 200);
            this.slidePic.x = 413; this.slidePic.y = 800;
        }
        else {
            egret.Tween.pauseTweens(this.slide2);
            this.slide2.alpha = 0;

            egret.Tween.resumeTweens(this.slide1);
            this.slide1.alpha = 1;
            this.slidePic.y = 600;

            this.slidePic.alpha = 0;
            egret.Tween.get(this.slidePic).to({ alpha: 1 }, 200);
            this.slidePic.x = 605; this.slidePic.y = 500;
        }
    }
    private Again() {
        // this.CloseTheDoor();
        // this.lantern.Back();

        // setTimeout(function(){
        //     egret.Tween.resumeTweens(myThis.slide1);
        // },2500);

        this.DO.Reset();
        var myThis = this;
        this.OpenTheDoor(new LoadEvent(""));

    }
    private CloseTheDoor() {
        egret.Tween.get(this.door_left).wait(1500).to({ x: 0 }, 1000, egret.Ease.cubicInOut);
        egret.Tween.get(this.door_right).wait(1500).to({ x: 0 }, 1000, egret.Ease.cubicInOut);
        var myThis = this;
    }

    private OpenTheDoor(evt: LoadEvent) {
        // this.sound.play(2,1);
        egret.Tween.get(this.slidePic).to({ alpha: 0 }, 500, egret.Ease.cubicOut);

        egret.Tween.pauseTweens(this.slide1); this.slide1.alpha = 0;
        egret.Tween.pauseTweens(this.slide2); this.slide2.alpha = 0;

        egret.Tween.get(this.door_left).wait(500).to({ x: -this.door_left.width / 2 }, 1000, egret.Ease.cubicInOut);
        egret.Tween.get(this.door_right).wait(500).to({ x: this.door_right.width / 2 }, 1000, egret.Ease.cubicInOut);
        this.cup.Appearance();
        var myThis = this;
        setTimeout(function () {
            myThis.DO.CanShake();
            setTimeout(function () {
                egret.Tween.get(myThis.phone).to({ alpha: 1 }, 500, egret.Ease.cubicOut);
                egret.Tween.get(myThis.word1).to({ alpha: 1 }, 500, egret.Ease.cubicOut);
                egret.Tween.get(myThis.word2).to({ alpha: 1 }, 500, egret.Ease.cubicOut);
            }, 500)
        }, 1000);
    }

    private BeginDraw(evt: LoadEvent) {
        // this.sound.play(2,1);
        //**********
        var myThis = this;
        this.cup.Shake();
        MyData.resultNo = Math.round(Math.random() * 7 + 1);
        setTimeout(function () {
            egret.Tween.get(myThis.word1).to({ alpha: 0 }, 500, egret.Ease.cubicOut);
            egret.Tween.get(myThis.phone).to({ alpha: 0 }, 500, egret.Ease.cubicOut);
        }, 500)
        this.drawhint.alpha = 0;
        egret.Tween.get(this.drawhint).wait(1000).to({ alpha: 1 }, 500, egret.Ease.cubicOut);

        this.drawhint.alpha = 0;
        egret.Tween.get(this.drawhand).wait(1000).to({ alpha: 1 }, 500, egret.Ease.cubicOut);
        //**********
    }
    private GoEnd(e: LoadEvent) {

        egret.Tween.get(this.word2).to({ alpha: 0 }, 500, egret.Ease.cubicOut);
        egret.Tween.get(this.drawhint).to({ alpha: 0 }, 500, egret.Ease.cubicOut);
        egret.Tween.get(this.drawhand).to({ alpha: 0 }, 500, egret.Ease.cubicOut);
        var myThis = this;
        setTimeout(function () {
            console.log("GoEnd");
            if (myThis.endpart.parent) { myThis.removeChild(myThis.endpart); }
            myThis.addChild(myThis.endpart);

        }, 700);
    }


}
declare function StartMusic()